package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.user.impl.dao.AppUserRoleDao;
import com.jiantong.user.model.AppUserRole;
import com.jiantong.user.service.AppUserRoleReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 4/7/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class AppUserRoleReadServiceImpl implements AppUserRoleReadService {

    private final AppUserRoleDao appUserRoleDao;
    @Autowired
    public AppUserRoleReadServiceImpl(AppUserRoleDao appUserRoleDao){
        this.appUserRoleDao = appUserRoleDao;
    }

    @Override
    public Response<AppUserRole> findAppUserRoleById(Long id) {
        try{
            AppUserRole appUserRole = appUserRoleDao.findById(id);
            if(appUserRole == null){
                log.error("failed to find app user role id = {}",id);
                return Response.fail("failed to find app user role");
            }
            return Response.ok(appUserRole);
        }catch (Exception e){
            log.error("failed to find app user role id = {}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find app user role");
        }
    }

    @Override
    public Response<AppUserRole> findByUserId(Long userId) {
        try{
            AppUserRole appUserRole = appUserRoleDao.findByUserId(userId);
            if(appUserRole == null){
                log.error("failed to find app user role userId = {}",userId);
                return Response.fail("failed to find app user role");
            }
            return Response.ok(appUserRole);
        }catch (Exception e){
            log.error("failed to find app user role userId = {}",userId, Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find app user role");
        }
    }

    @Override
    public Response<List<AppUserRole>> findAppUserRolesByUserId(Long userId) {
        try{
            List<AppUserRole> appUserRoles = appUserRoleDao.findAppUserRoles(userId);
            if(appUserRoles == null){
                log.error("failed to find app user roles userId = {}",userId);
                return Response.fail("failed to find app user roles");
            }
            return Response.ok(appUserRoles);
        }catch (Exception e) {
            log.error("failed to find app user role userId = {}", userId, Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find app user roles");
        }
    }

    @Override
    public Response<Paging<AppUserRole>> pagingAppUserRoles(Integer pageNo, Integer pageSize, Long userId ,List<Integer> statuses) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String ,Object> map = Maps.newHashMap();
            if(userId != null){
                map.put("userId",userId);
            }
            if(statuses != null && !statuses.isEmpty()){
                map.put("statuses",statuses);
            }
            return Response.ok(appUserRoleDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to paging AppUserRole, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("AppUserRole.paging.fail");
        }
    }

    /**
     * 分页查询 只查询符合指派案件办理的人员, 用来找到那些可以办案的人员,把案件指派给他们 具体查询条件
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @param roles    参与查询的角色类型
     * @return Paging<AppUserRole>
     */
    @Override
    public Response<Paging<AppUserRole>> pagingLawAppUserRoles(Integer pageNo, Integer pageSize, List<Integer> roles) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String ,Object> map = Maps.newHashMap();
            if(roles != null){
                map.put("status",1);
                map.put("roles",roles);
            }
            return Response.ok(appUserRoleDao.pagingLaw(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to paging AppUserRole, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("AppUserRole.paging.fail");
        }
    }
}
